package com.newidea.facillty.component;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.newidea.facillty.service.DatunService;
import com.newidea.facillty.service.HuaweiOpcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class HeatScheduled {

    @Value("${collect.modbus.company}")
    private String company;
    @Value("${pushUrl}")
    private String pushUrl;
    @Value("${hw.opc.number}")
    private String hwOpcNumber;

    private final DatunService datunService;
    private final HuaweiOpcService huaweiOpcService;

    public HeatScheduled(
            DatunService datunService, HuaweiOpcService huaweiOpcService
    ) {
        this.datunService = datunService;
        this.huaweiOpcService = huaweiOpcService;
    }


    /**
     * 每隔5分钟执行一次
     */
/*    @Scheduled(cron = "0 0/5 * * * *")
    public void reviseModbusTcp() {
        // 采集ModbusTCP中的数据
        JSONObject collect = new JSONObject();

        if ("datun".equals(company)) {
            collect = datunService.collect();
            collect.put("company", "大屯");
        }

        // 推送到116的服务器接收
        HttpUtil.post(pushUrl + "/receiveTcp", collect.toString());
    }*/


    /**
     * 华威OPC换热站
     */
    @Scheduled(cron = "0 0/6 * * * *")
    public void reviseHwOpc() throws Exception {
        // 采集ModbusTCP中的数据
        JSONObject opcAllTag = huaweiOpcService.getOpcAllTag();
        opcAllTag.put("hwOpcNumber", hwOpcNumber);
        log.info("华威{}号换热站数据采集成功", hwOpcNumber);

        // 推送到116的服务器接收
        HttpUtil.post(pushUrl + "/reviseHwOpc", opcAllTag.toString());
        log.info("数据推送成功");
    }

}
